<?php
/**
 * Created by PhpStorm.
 * User: mars
 * Date: 2018/6/22
 * Time: 下午8:57
 */

namespace common\models;


class Photoes extends \backend\models\Photoes {

	public function scenarios(){
		return [
			'update'=>['title','photo','update_time'],
			'change'=>['title'],
			'like'=>['likes'],
		];
	}

	public function updateImage($date,$scenario='update'){
		$this->scenario = $scenario;
		//图片路径
		$path = \Yii::$app->basePath.'/uploads/photoes/';
		if($this->load($date)){
			if ($this->imageTitle != null){
				$this->title = $this->imageTitle;
			}
			//返回文件实例
			$this->imageFile = UploadedFile::getInstance($this,'imageFile');
			//获取当前时间
			$datatime = new \DateTime;
			$session = \Yii::$app->session;
			$imageName = $datatime->format('YmdHis').$session['name'].'.'.$this->imageFile->extension;
			$srcImage = $path.$imageName;
			if ($this->imageFile->saveAs($srcImage)){
				$this->photo = $imageName;
				$this->uid = $session['uid'];
				$this->update_time = $datatime->format('Y-m-d H-i-s');
				return $this->save();
			}
		}
		return false;
	}

	public function showByTime(){
		$result = $result = static::find()->orderBy('update_time DESC')->all();
		return $result;

	}

	public function showByLikes(){
		$result =  $result = static::find()->orderBy('likes DESC')->all();
		return $result;
	}

	public function showByFellow($uid){
		$fellow = new Fellow();
		$fellows = $fellow->findFellowByUId($uid);
		$f_uid = [];
		foreach ($fellows as $item){
			array_push($f_uid,$item->f_uid);
		}
		$result = self::find()->where(['uid'=>$f_uid])->orderBy('update_time DESC')->all();
		return $result;

	}

	public function showByUid($uid){
		$result = static::find()->where(['uid'=>$uid])->orderBy('update_time DESC')->all();
		return $result;
	}

	public function getFellow(){

		return $this->hasMany(Fellow::className(),['f_uid'=>'uid']);

	}



	/**
	 * Returns static class instance, which can be used to obtain meta information.
	 *
	 * @param bool $refresh whether to re-create static instance even, if it is already cached.
	 *
	 * @return static class instance.
	 */
	public static function instance( $refresh = false ) {
		// TODO: Implement instance() method.
}}